
//警告功能 - start
/**
 * this is a method replacing the alert and confirm
 * @param  {object or string} options       the content of alert things
 * @param  {number} opt_warntype  chooce alert type or confirm type or original
 * @param  {function} opt_callback1 return function of confirm
 * @param  {function} opt_callback2 return function of cancle
 * @return {html}               change the alert style
 */
function alertMs(options, opt_warntype, opt_callback1, opt_callback2) {
    var option = {
        warnType: 1, //1 为alert型; 2 为confirm型; 3 为系统alert型
        Title: "消息",
        Message: "错误",
        theme: "#850b06" //"red","blue","green","yellow"可以在generateCSS的themes添加对象
    };
    var generate = {
        HTML: function (option) {
            var $html = "" +
                "<div id=\"msOut\">" +
                "    <div id=\"msBox\">" +
                "        <div class=\"msTitle\">" + option.Title + "</div>" +
                "        <div class=\"msMessage\">" + option.Message + "</div>" +
                "        <div class=\"msBtn\">" +
                "            <span class=\"msConfirm\">确定</span>" +
                (option.warnType == 2 ? "<span class=\"msReject\">取消</span>" : "") +
                "        </div>" +
                "    </div>" +
                "    <div class=\"msLayer\"></div>" +
                "</div>";
            $('body').append($html);
            this.CSS(option);
        },
        CSS: function (option) {
            var themes = {
                "red": "#CC3300",
                "blue": "#99CCFF",
                "green": "#00FF99",
                "yellow": "#FFFF66"
            };
            var themeColor = themes[option.theme];
            for (var color in themes) {
                if (color == option.theme) {
                    themeColor = themes[color];
                    break;
                } else themeColor = option.theme;
            }
            $("#msOut").css({
                "width": '100%',
                "height": '100%',
                "zIndex": '99999',
                "position": 'fixed',
                "top": '0',
                "left": '0',
                "font-size": '16px'
            });
            $(".msLayer").css({
                "width": '100%',
                "height": '100%',
                "filter": 'Alpha(opacity=40)',
                "backgroundColor": '#000',
                "opacity": '0.4'
            });
            $("#msBox").css({
                "width": '500px',
                "height": '300px',
                "zIndex": '99999',
                "position": 'absolute',
                "opacity": "0"
            });
            $(".msTitle").css({
                "display": 'block',
                "fontSize": '14px',
                "color": '#444',
                "padding": '10px 15px',
                "backgroundColor": '#f0f4f7',
                "borderRadius": '15px 15px 0 0',
                "borderBottom": '3px solid ' + themeColor,
                "fontWeight": 'bold'
            });
            $(".msMessage").css({
                "padding": " 50px",
                "line-height": " 22px",
                "background-color": "#393D49",
                "color": "#fff",
                "font-weight": "300",
                "overflow": "hidden",
                "text-overflow": "ellipsis"
            });
            $(".msBtn").css({
                "padding": '15px 0 10px 0',
                "borderRadius": '0 0 15px 15px',
                "textAlign": 'center',
                "background-color": "#f0f4f7"
            });
            $(".msConfirm").css({
                "display": "inline-block",
                "height": "28px",
                "line-height": "28px",
                "margin": "0 6px",
                "padding": "0 15px",
                "border": "1px solid" + themeColor,
                "background-color": themeColor,
                "color": " #fff",
                "border-radius": "2px",
                "font-weight": "400",
                "cursor": "pointer",
                "text-decoration": "none"
            });
            $(".msReject").css({
                "display": "inline-block",
                "height": "28px",
                "line-height": "28px",
                "margin": "0 6px",
                "padding": "0 15px",
                "border": "1px solid #dedede",
                "background-color": "#f1f1f1",
                "color": "#333",
                "border-radius": "2px",
                "font-weight": "400",
                "cursor": "pointer",
                "text-decoration": "none"
            });
            this.Event(opt_callback1, opt_callback2);
        },
        Event: function (opt_callback1, opt_callback2) {
            var pathname = window.location.pathname.split('/')[2];
            var $width = '',
                $height = '';
            if (!!pathname) {
                $width = window.parent.document.documentElement.clientWidth - $('.aside', parent.document).width(); //屏幕宽
                $height = window.parent.document.documentElement.clientHeight; //屏幕高
            } else {
                $width = document.documentElement.clientWidth; //屏幕宽
                $height = document.documentElement.clientHeight; //屏幕高
            }
            var boxWidth = $("#msBox").width();
            var boxHeight = $("#msBox").height();
            $("#msBox").css({
                "left": ($width - boxWidth) / 2 + "px"
            });
            $("#msBox").stop().animate({
                "top": ($height - boxHeight) / 2 + "px",
                "left": ($width - boxWidth) / 2 + "px",
                "opacity": "1"
            }, 300);
            $(".msConfirm").click(function () {
                $("#msBox").stop().animate({
                    "top": "0",
                    "opacity": "0.2"
                }, 300, function () {
                    $("#msOut").remove();
                });
                if (typeof opt_callback1 === "function") {
                    opt_callback1();
                    $('.msOut').remove()
                }
            });
            $(".msReject").click(function () {
                $("#msBox").stop().animate({
                    "top": "0",
                    "opacity": "0.2"
                }, 300, function () {
                    $("#msOut").remove();
                });
                if (typeof opt_callback2 === "function") {
                    opt_callback2();
                    $('.msOut').remove()
                }
            });
        }
    }
    if (typeof options === "string") {
        option.Message = options;
        if (typeof opt_warntype === "number") {
            if (opt_warntype != 3) {
                option = $.extend(option, {
                    warnType: opt_warntype
                });
                generate.HTML(option);
            } else {
                alert(option.Message);
            }
        } else if (typeof opt_warntype === "function") {
            opt_callback1 = opt_warntype;
            generate.HTML(option);
        } else {
            generate.HTML(option);
        }
    } else if (typeof options === "object") {
        option = $.extend(option, options);
        if (typeof opt_warntype === "number") {
            if (opt_warntype != 3) {
                option = $.extend(option, {
                    warnType: opt_warntype
                });
                generate.HTML(option);
            } else {
                alert(option.Message);
            }
        } else if (typeof opt_warntype === "function") {
            option = $.extend(option, options);
            opt_callback1 = opt_warntype;
            generate.HTML(option);
        } else {
            generate.HTML(option);
        }
    }
}
//end - 警告功能